package com.example.bledemo.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * IP设置管理工具类
 */
public class IpSettingsManager {
    
    private static final String PREF_NAME = "ip_settings";
    private static final String KEY_IP_ADDRESS = "ip_address";
    private static final String KEY_PORT = "port";
    private static final String BASE_URL = "base_url";
    private static final String FIRST_BOOT = "first_boot";

    /**
     * 保存IP设置
     */
    public static void saveIpSettings(Context context, String ipAddress, int port) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(KEY_IP_ADDRESS, ipAddress);
        editor.putString(BASE_URL, "http://"+ipAddress+":"+port+"/api/portal/");
        editor.putInt(KEY_PORT, port);
        editor.putBoolean(FIRST_BOOT, true);
        editor.apply();
    }
    /**
     * 获取保存的IP地址
     */
    public static String getBaseUrl(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return prefs.getString(BASE_URL, "");
    }
    /**
     * 获取保存的IP地址
     */
    public static String getIpAddress(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return prefs.getString(KEY_IP_ADDRESS, "");
    }
    
    /**
     * 获取保存的端口号
     */
    public static int getPort(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return prefs.getInt(KEY_PORT, 0);
    }
    /**
     * 获取保存的IP地址
     */
    public static Boolean getFirstBoot(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return prefs.getBoolean(FIRST_BOOT, false);
    }
    /**
     * 检查是否有保存的IP设置
     */
    public static boolean hasIpSettings(Context context) {
        String ipAddress = getIpAddress(context);
        int port = getPort(context);
        return !ipAddress.isEmpty() && port > 0;
    }
    
    /**
     * 清除IP设置
     */
    public static void clearIpSettings(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.clear();
        editor.apply();
    }
} 